//  this library.
import 'package:hktk_app/model/course/note_video_model.dart';
import 'package:hktk_app/model/home/resource_model.dart';
import 'package:hktk_app/model/my/note_chapter_model.dart';
import 'package:hktk_app/model/subject/note_model.dart';
import 'package:hktk_app/model/subject/note_vo_model.dart';
import 'package:hktk_app/model/subject/paper_model.dart';
import 'package:hktk_app/model/subject/upload_collection_model.dart';

final dynamic _factories = <Type, Function>{
  NoteModel: (Map<String, dynamic> json) => NoteModel.fromJson(json),
  ResourceModel: (Map<String, dynamic> json) => ResourceModel.fromJson(json),
  UploadCollectionModel: (Map<String, dynamic> json) =>
      UploadCollectionModel.fromJson(json),
  NoteVoModel: (Map<String, dynamic> json) => NoteVoModel.fromJson(json),
  PaperModel: (Map<String, dynamic> json) => PaperModel.fromJson(json),
  NoteChapterModel: (Map<String, dynamic> json) =>
      NoteChapterModel.fromJson(json),
  NoteVideoModel: (Map<String, dynamic> json) => NoteVideoModel.formJson(json),
};

final dynamic _factoriesJson = <Type, Function>{
  NoteModel: (NoteModel model) => model.toJson(),
  ResourceModel: (ResourceModel model) => model.toJson(),
  UploadCollectionModel: (UploadCollectionModel model) => model.toJson(),
  NoteVoModel: (NoteVoModel model) => model.toJson(),
  PaperModel: (PaperModel model) => model.toJson(),
  NoteChapterModel: (NoteChapterModel model) => model.toJson(),
  NoteVideoModel: (NoteVideoModel model) => model.toJson(),
};

/// 返回数据分页方法
class ResourcePageModel<T> {
  int? size;
  int? total;
  List<T>? records;
  int? current;
  int? pages;

  ResourcePageModel({
    this.size,
    this.total,
    this.records,
    this.current,
    this.pages,
  });

  ResourcePageModel.fromJson(Map<String, dynamic> json) {
    size = json["size"];
    total = json["total"];
    if (json["records"] != null) {
      records = <T>[];
      json["records"].forEach((v) {
        records!.add(_factories[T]!(v));
      });
    }
    current = json["current"];
    pages = json["pages"];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data["size"] = size;
    data["total"] = total;
    if (records != null) {
      data["records"] = records!.map((v) => _factoriesJson[T]!(v)).toList();
    }
    data["current"] = current;
    data["pages"] = pages;
    return data;
  }
}
